<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="180"
			label-align="left">
			<u-divider color="#fa3534">立项信息</u-divider>
			<u-form-item label="项目编号" v-if="judgeShow('yq002')">
				<u-input v-model="dataForm.yq002" placeholder="项目编号" disabled></u-input>
			</u-form-item>
			<u-form-item label="项目名称" type="textarea" v-if="judgeShow('yq005')">
				<u-input v-model="dataForm.yq005" placeholder="项目名称" disabled></u-input>
			</u-form-item>
			<u-form-item label="优先级" type="textarea" v-if="judgeShow('yq025')">
				<u-tag v-if='dataForm.yq025==1' text="常规" type="primary" />
				<u-tag v-if='dataForm.yq025==2' text="优先" type="warning" />
				<u-tag v-if='dataForm.yq025==3' text="重点" type="error" />
			</u-form-item>
			<u-form-item label="项目经理" type="textarea" v-if="judgeShow('yq026')">
				<view v-for="(item,index) in yq026Name" :key="index" style="margin-left: 5px;">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
			<u-form-item label="客户名称" type="textarea" v-if="judgeShow('yq006')">
				<u-input v-model="dataForm.yq006" placeholder="客户名称" disabled></u-input>
			</u-form-item>
			<u-form-item label="业务负责人" type="textarea" v-if="judgeShow('yq027')">
				<view v-for="(item,index) in yq027Name" :key="index" style="margin-left: 5px;">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
			<u-form-item label="项目地点" type="textarea" v-if="judgeShow('yq008')">
				<u-input v-model="dataForm.yq008" placeholder="项目地点" disabled></u-input>
			</u-form-item>
			<u-form-item label="项目类型" type="textarea" v-if="judgeShow('yq028')">
				{{formattedsortCode(dataForm.yq028)}}
			</u-form-item>
			<u-form-item label="发货日期" v-if="judgeShow('yq009')">
				<span>{{formattedData(dataForm.yq009)}}</span>
			</u-form-item>
			<u-form-item label="交付日期" v-if="judgeShow('yq010')">
				<span>{{formattedData(dataForm.yq010)}}</span>
			</u-form-item>
			<u-form-item label="关联计划" v-if="judgeShow('yq020')">
				<span>{{dataForm.yq020}}</span>
			</u-form-item>
			<u-form-item label="计划开始时间">
				<span>{{formattedTime(dataForm.planStartTime)}}</span>
			</u-form-item>
			<u-form-item label="计划结束时间">
				<span>{{formattedTime(dataForm.planEndTime)}}</span>
			</u-form-item>
			<u-form-item label="实际开始时间">
				<span>{{formattedTime(dataForm.yq023)}}</span>
			</u-form-item>
			<u-form-item label="实际完成时间">
				<span>{{formattedTime(dataForm.yq029)}}</span>
			</u-form-item>
			<u-divider color="#fa3534">电缆敷设表</u-divider>
			<u-form-item label="附件" v-if="judgeShow('annex01')">
				<jnpf-file v-model="dataForm.annex01" disabled />
			</u-form-item>
			<u-form-item label="备注" type="textarea" v-if="judgeShow('yq030')">
				<u-input v-model="dataForm.yq030" placeholder="备注"></u-input>
			</u-form-item>
			<u-form-item label="评审状态" type="textarea" v-if="judgeShow('yq017')">
				<u-radio-group v-model="dataForm.yq017">
					<u-radio name="1">
						未评审
					</u-radio>
					<u-radio name="2">
						已评审
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="会议纪要" type="textarea" v-if="judgeShow('yq018')">
				<u-input v-model="dataForm.yq018" placeholder="会议纪要" :disabled="judgeWrite('yq018')"></u-input>
			</u-form-item>
			<u-form-item label="任务处理人" type="textarea" v-if="judgeShow('yq021')">
				<view v-for="(item,index) in yq021Name" :key="index" style="margin-left: 5px;">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
			<u-form-item label="责任人" type="textarea" v-if="judgeShow('yq024')">
				<view v-for="(item,index) in yq024Name" :key="index" style="margin-left: 5px;">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
		</u-form>
	</view>
</template>
<script>
	import {
		getPjsyq
	} from '@/api/workFlow/flowEngine'
	import {
		getUserAll,
		Interface,
		getDictionaryDataSelector
	} from '@/api/common'
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update
	} from '@/api/apply/order'
	export default {
		name: 'crmOrder',
		mixins: [comMixin],
		data() {
			return {
				childrenShow: false,
				childrenName: '',
				dataForm: {},
				tableField133: [],
				info: {},
				id: '',
				yq026Name: [],
				yq027Name: [],
				yq021Name: [],
				yq024Name: [],
				nameList: [],
				xx032List: [],
				tn003List: [],
				childrenList: [],
			}
		},

		methods: {
			init(data, id, flowFormContentJson) {
				this.setting = data
				this.id = id
				this.getInfo(data)
			},
			childNodes(item) {
				if (item.children.length == 0) return
				this.childrenList = item.children;
				this.childrenName = item.tb004 + '-子节点';
				this.childrenShow = true;
			},
			getDynamicStyle(item) {
				return {
					'background-color': item.children && item.children.length > 0 ? 'aqua' : ''
				}
			},
			// 转换时间到时
			formattedTime(time = '0000000000') {
				if (!time) return '';
				return time.slice(0, 4) + '-' + time.slice(4, 6) + '-' + time.slice(6, 8) + ' ' + time.slice(8, 10);
			},
			// 转换时间到日
			formattedData(data = '00000000') {
				if (!data) return '';
				return data.slice(0, 4) + '-' + data.slice(4, 6) + '-' + data.slice(6, 8)
			},
			formattedName(id) {
				// 找到匹配的ID并返回名字
				const person = this.nameList.find(p => p.id === id);
				return person ? person.realName : '';
			},
			formattedtn003(tn003) {
				// 找到匹配的ID并返回名字
				const obj = this.tn003List.find(p => p.id === tn003);
				return obj ? obj.fullName : '';
			},
			formattedsortCode(xx032) {
				const obj = this.xx032List.find(p => p.sortCode == xx032);
				return obj ? obj.fullName : '';
			},
			getInfo(data) {
				getPjsyq(data.id).then(res => {
					if (res.code == 200) {
						this.dataForm = res.data
						this.getUser()
						this.getInterface()
						this.getxz008Options()
					}
				})
			},
			// 获取项目类型
			getxz008Options() {
				getDictionaryDataSelector("505518848196226117").then((res) => {
					this.xx032List = res.data.list;
				});
			},
			// 获取优先级
			getxz009Options() {
				getDictionaryDataSelector("505518936847035461").then((res) => {

				});
			},
			getUser() {
				getUserAll().then(res => {
					this.nameList = res.data
					this.yq026Name = res.data.filter(item => this.dataForm.yq026.includes(item.id)).map(item => item
						.realName);
					this.yq027Name = res.data.filter(item => this.dataForm.yq027.includes(item.id)).map(item => item
						.realName);
					this.yq021Name = res.data.filter(item => this.dataForm.yq021.includes(item.id)).map(item => item
						.realName);
					this.yq024Name = res.data.filter(item => this.dataForm.yq024.includes(item.id)).map(item => item
						.realName);
				})
			},
			getInterface() {
				Interface().then(res => {
					this.tn003List = res.data
				})
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>